package com.example.emailWeb.ntru.encrypt;

import com.example.emailWeb.ntru.polynomial.IntegerPolynomial;
import com.example.emailWeb.ntru.polynomial.SparseTernaryPolynomial;

import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.Arrays;

public class Test {
    public  static IntegerPolynomial generateR(){
        int N = 10,q = 2048;
        while(true) {
            IntegerPolynomial er = SparseTernaryPolynomial.generateRandom(N, N / 3, N / 3 - 1, new SecureRandom()).toIntegerPolynomial();
//            byte[] sData = getSeed(m, PKA, b);
            //从多项式环 Ř 中随机选取小多项式 r.
//            er = generateBlindingPoly(sData).toIntegerPolynomial();
            //需要保证r (mod q)可逆；
            IntegerPolynomial rCopy = er;
            IntegerPolynomial invertFq = rCopy.invertFq(q);
            if(invertFq != null){
                return er;
            }
        }
    }
    public static void main(String[] args) {
        /**
         * int 转成4个字节
         * 在byte[] 前，添加4个字节
         * 截取后面的字节
         */

        byte[] arr = {1,2,3,4,13,12,21,3,23,21,3,23,2};
        int l = arr.length;
        ByteBuffer mBuf = ByteBuffer.allocate(arr.length+1);
        mBuf.put((byte)l);
        mBuf.put(arr);
        byte[] cM = mBuf.array();
        System.out.println(Arrays.toString(cM));

        ByteBuffer buf = ByteBuffer.wrap(cM);
        int mLen = buf.get() & 0xFF;   // llen=1, so read one byte; 代表明文的消息的字节长度；
        System.out.println("m_len: "+mLen);
        byte[] cm = new byte[mLen];
        buf.get(cm); // 得到明文
        System.out.println("cm: "+Arrays.toString(cm));
    }
}
